// /src/pages/hot/hot.vue
<script setup lang="ts">
import { getHotdetailApi, getHotProductListApi } from '@/services/hotApi'
import { onLoad } from '@dcloudio/uni-app'
import { ref, computed } from 'vue'
import { BaseUrl } from '@/utils/request'
import type { ProductType, HotRecommenditemType } from '@/types/home'
// 热门推荐页 标题和url
const hotMap = [
  { type: '1', title: '精选全攻略', url: '/hot/strategy' },
  { type: '2', title: '最受欢迎', url: '/hot/popular' },
  { type: '3', title: '精心优选', url: '/hot/optimization' },
  { type: '4', title: '生活加分项', url: '/hot/life' },
]
// uniapp提供的defineProps还可以接受收页面传递的参数，地址栏中的参数
const query = defineProps<{
  type: number
  id: number
}>()
const hotId = ref(1)
// 选中效果
const active = ref(0)
// 页面加载时就会请求第一页的数据，所以页码从2开始
const page = ref(2)
// 总数
const count = ref(0)
// 加在完成
const isFinished = ref(false)
const maxpage = computed(() => Math.ceil(count.value / 4))
const hotDetailList = ref<HotRecommenditemType<ProductType>[]>()
const hotProductList = ref<ProductType[]>()
const getHotdetailData = async (id: number) => {
  const res = await getHotdetailApi(id)
  hotDetailList.value = res.data
  count.value = res.count!
  // 拿到当前推荐id后，再去请求数据
  hotId.value = res.data![0].id
  console.log(hotId.value)
}
const getHotProductListData = async (id: number) => {
  const res = await getHotProductListApi(id, 1)
  hotProductList.value = res.data
}
// 选中效果，请求数据，并该修改当前推荐对象id,还需要清空数据
const getproductAndActive = async (index: number, id: number) => {
  hotProductList.value = []
  hotId.value = id
  page.value = 1
  active.value = index
  const res = await getHotProductListApi(id, page.value)
  hotProductList.value = res.data
  count.value = res.count!
  page.value++
}
const onscrolltolower = async () => {
  console.log(maxpage.value)
  if (page.value > maxpage.value) {
    return uni.showToast({
      title: '没有更多数据了',
      icon: 'none',
    })
  }
  const res = await getHotProductListApi(hotId.value, page.value)
  hotProductList.value!.push(...res.data!)
  count.value = res.count!
  page.value++
}
onLoad(async () => {
  await getHotdetailData(query.type)
  await getHotProductListData(hotId.value)
})
</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image
        src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-05-20/84abb5b1-8344-49ae-afc1-9cb932f3d593.jpg"
      ></image>
    </view>
    <!-- 推荐选项 -->
    <view class="tabs">
      <text
        class="text"
        v-for="(item, index) in hotDetailList"
        :key="item.id"
        :class="active === index ? 'active' : ''"
        @tap="getproductAndActive(index, item.id)"
        >{{ item.name }}</text
      >
    </view>
    <!-- 推荐列表 -->
    <scroll-view scroll-y class="scroll-view" @scrolltolower="onscrolltolower">
      <view class="goods">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="item in hotProductList"
          :key="item.id"
          :url="`/pages/goods/goods?goods_id=${item.id}`"
        >
          <image class="thumb" :src="`${BaseUrl}/${item.cover}`"></image>
          <view class="name ellipsis">{{ item.description }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ item.current_price }}</text>
          </view>
        </navigator>
      </view>
      <view class="loading-text">{{ isFinished ? '正在加载...' : '没有更多数据啦！' }}</view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f4f4f4;
}
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}
.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
}
.scroll-view {
  flex: 1;
}
.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
  .text {
    margin: 0 20rpx;
    position: relative;
  }
  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      transform: translate(-50%);
      background-color: #27ba9b;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
  .navigator {
    width: 345rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .thumb {
    width: 305rpx;
    height: 305rpx;
  }
  .name {
    height: 88rpx;
    font-size: 26rpx;
  }
  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }
  .symbol {
    font-size: 70%;
  }
  .decimal {
    font-size: 70%;
  }
}

.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>
